/*
 * Copyright 2007 The semap project development team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by myn at 07.08.2007 17:03:22.
 *
 * TODO <<insert description>>.
 *
 * $Id: Contact.java 434 2007-08-24 12:00:45Z Mike.Navern $
 */
package com.google.code.contactserv.model;

import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * @author myn
 */
public class Contact {

    private String uri; // Contact identifier
    private Resource resource;


    public Contact() {
    }

    /**
     * @param model jena model
     * @param uri URI
     */
    public Contact(Model model, String uri) {
        super();
        this.uri = uri;
        this.resource = model.createResource(uri);
    }


    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    /**
     * Check if the literal value is in one of chosen language
     * @return true if success, false - otherwise
     */
    protected boolean chosenLanguage(Literal l, String[] langs) {
        if (langs.length == 0) {
        	return true;
		} 
		for (int i = 0; i < langs.length; i++) {
			if (l.getLanguage().equalsIgnoreCase(langs[i])) {
	        	return true;
			}
		}
		return false;
    }
    
    /**
     * Prepare array of requested languages
     * @return true if success, false - otherwise
     */
    protected String[] prepareLanguages(String langs) {
        return langs.isEmpty() ? new String[0] : langs.split(",\\s*");
    }

    public void setProp(Property prop, String value) {
        resource.addProperty(prop, value);
    }

    public void setProp(Property prop, String value, String lang) {
        resource.addProperty(prop, value, lang);
    }

    /**
     * @return list of selected property values 
     */
    public List<String> getProps(Property prop) {
    	return getProps(prop, "");
    }
    
    /**
     * @return list of selected property values in specified languages
     */
    public List<String> getProps(Property prop, String lang) {
        List<String> res  = new ArrayList<String>();
        StmtIterator iter = resource.listProperties(prop);

        String[] langs = prepareLanguages(lang);
        
        try { 
            while(iter.hasNext()) {
                Literal l = iter.nextStatement().getLiteral();
                if (chosenLanguage(l, langs)) {
                	res.add(l.toString());
				}
            }
        }
        finally { 
            iter.close(); 
        }
        return res;
    }

    /**
     * @return selected property value
     */
    public String getProp(Property prop) {
    	return getProp(prop, "");
    }
    
    /**
     * @return selected property value in specified languages
     */
    public String getProp(Property prop, String lang) {
        String ret = "";
        StmtIterator iter = resource.listProperties(prop);

        String[] langs = prepareLanguages(lang);

        try { 
            while(iter.hasNext()) {
                Literal l = iter.nextStatement().getLiteral();
                if (chosenLanguage(l, langs)) {
                    ret = l.toString();
                    break;
				}
            }
        }
        finally { 
            iter.close(); 
        }
        return ret;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return getProp(FOAF.gender);
    }
    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        resource.addProperty(FOAF.gender, gender);
    }
    /**
     * @return the nick
     */
    public String getNick() {
        return getProp(FOAF.nick);
    }

    public String getNick(String lang) {
        return getProp(FOAF.nick, lang);
    }

    public List<String> getNicks() {
        return getProps(FOAF.nick);
    }

    public List<String> getNicks(String lang) {
        return getProps(FOAF.nick, lang);
    }
    /**
     * @param nick the nick to set
     */
    public void setNick(String nick) {
        resource.addProperty(FOAF.nick, nick);
    }

    public void setNick(String nick, String lang) {
        resource.addProperty(FOAF.nick, nick, lang);
    }
    /**
     * @return the name
     */
    public String getName() {
//        return resource.getProperty(FOAF.name).getString();
        return getProp(FOAF.name);
    }

    public String getName(String lang) {
        return getProp(FOAF.name, lang);
    }

    public List<String> getNames() {
        return getProps(FOAF.name);
    }

    public List<String> getNames(String lang) {
        return getProps(FOAF.name, lang);
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        resource.addProperty(FOAF.name, name);
    }

    public void setName(String name, String lang) {
        resource.addProperty(FOAF.name, name, lang);
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return getProp(FOAF.firstName);
    }

    public String getFirstname(String lang) {
        return getProp(FOAF.firstName, lang);
    }

    public List<String> getFirstnames() {
        return getProps(FOAF.firstName);
    }

    public List<String> getFirstnames(String lang) {
        return getProps(FOAF.firstName, lang);
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        resource.addProperty(FOAF.firstName, firstname);
    }

    public void setFirstname(String firstname, String lang) {
        resource.addProperty(FOAF.firstName, firstname, lang);
    }

    /**
     * @return the givenname
     */
    public String getGivenname() {
        return getProp(FOAF.givenname);
    }

    public String getGivenname(String lang) {
        return getProp(FOAF.givenname, lang);
    }

    public List<String> getGivennames() {
        return getProps(FOAF.givenname);
    }

    public List<String> getGivennames(String lang) {
        return getProps(FOAF.givenname, lang);
    }

    /**
     * @param firstname the givenname to set
     */
    public void setGivenname(String givenname) {
        resource.addProperty(FOAF.givenname, givenname);
    }

    public void setGivenname(String givenname, String lang) {
        resource.addProperty(FOAF.givenname, givenname, lang);
    }
    /**
     * @return the surname
     */
    public String getSurname() {
        return getProp(FOAF.surname);
    }

    public String getSurname(String lang) {
        return getProp(FOAF.surname, lang);
    }

    public List<String> getSurnames() {
        return getProps(FOAF.surname);
    }

    public List<String> getSurnames(String lang) {
        return getProps(FOAF.surname, lang);
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        resource.addProperty(FOAF.surname, surname);
    }

    public void setSurname(String surname, String lang) {
        resource.addProperty(FOAF.surname, surname, lang);
    }

    /**
     * @return the birthday
     */
    public String getBirthday() {
        return getProp(FOAF.birthday);
    }
    /**
     * @param birthday the birthday to set
     */
    public void setBirthday(String birthday) {
        resource.addProperty(FOAF.birthday, birthday);
    }
    /**
     * @return the mailbox
     */
    public String getMailbox() {
        return getProp(FOAF.mbox);
    }

    public List<String> getMailboxs() {
        return getProps(FOAF.mbox);
    }
    /**
     * @param mailbox the mailbox to set
     */
    public void setMailbox(String mailbox) {
        resource.addProperty(FOAF.mbox, mailbox);
    }
    /**
     * @return the image
     */
    public String getImage() {
        return getProp(FOAF.img);
    }

    public List<String> getImages() {
        return getProps(FOAF.img);
    }
    /**
     * @param image the image to set
     */
    public void setImage(String image) {
        resource.addProperty(FOAF.img, image);
    }
    /**
     * @return the phone
     */
    public String getPhone() {
        return getProp(FOAF.phone);
    }

    public List<String> getPhones() {
        return getProps(FOAF.phone);
    }
    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        resource.addProperty(FOAF.phone, phone);
    }
    /**
     * @return the homepage
     */
    public String getHomepage() {
        return getProp(FOAF.homepage);
    }
    /**
     * @param homepage the homepage to set
     */
    public void setHomepage(String homepage) {
        resource.addProperty(FOAF.homepage, homepage);
    }
    /**
     * @return the weblog
     */
    public String getWeblog() {
        return getProp(FOAF.weblog);
    }
    /**
     * @param weblog the weblog to set
     */
    public void setWeblog(String weblog) {
        resource.addProperty(FOAF.weblog, weblog);
    }
    /**
     * @return the workpage
     */
    public String getWorkpage() {
        return getProp(FOAF.workplaceHomepage);
    }
    /**
     * @param workpage the workpage to set
     */
    public void setWorkpage(String workpage) {
        resource.addProperty(FOAF.workplaceHomepage, workpage);
    }
    /**
     * @return the icq
     */
    public String getIcq() {
        return getProp(FOAF.icqChatID);
    }
    /**
     * @param icq the icq to set
     */
    public void setIcq(String icq) {
        resource.addProperty(FOAF.icqChatID, icq);
    }
    /**
     * @return the aim
     */
    public String getAim() {
        return getProp(FOAF.aimChatID);
    }
    /**
     * @param aim the aim to set
     */
    public void setAim(String aim) {
        resource.addProperty(FOAF.aimChatID, aim);
    }
    /**
     * @return the msn
     */
    public String getMsn() {
        return getProp(FOAF.msnChatID);
    }
    /**
     * @param msn the msn to set
     */
    public void setMsn(String msn) {
        resource.addProperty(FOAF.msnChatID, msn);
    }
    /**
     * @return the yahoo
     */
    public String getYahoo() {
        return getProp(FOAF.yahooChatID);
    }
    /**
     * @param yahoo the yahoo to set
     */
    public void setYahoo(String yahoo) {
        resource.addProperty(FOAF.yahooChatID, yahoo);
    }
    /**
     * @return the jabber
     */
    public String getJabber() {
        return getProp(FOAF.jabberID);
    }
    /**
     * @param jabber the jabber to set
     */
    public void setJabber(String jabber) {
        resource.addProperty(FOAF.jabberID, jabber);
    }
    /**
     * @return the account
     */
    public String getAccount() {
        return getProp(FOAF.accountName);
    }

    public String getAccount(String lang) {
        return getProp(FOAF.accountName, lang);
    }

    public List<String> getAccounts() {
        return getProps(FOAF.accountName);
    }

    public List<String> getAccounts(String lang) {
        return getProps(FOAF.accountName, lang);
    }

    /**
     * @param account the account to set
     */
    public void setAccount(String account) {
        resource.addProperty(FOAF.accountName, account);
    }

    public void setAccount(String account, String lang) {
        resource.addProperty(FOAF.accountName, account, lang);
    }

    /*
    /**
     * @return the openid
     * /
    public String getOpenid() {
        return getProp(FOAF.openid);
    }
    /**
     * @param openid the openid to set
     * /
    public void setOpenid(String openid) {
        this.openid = openid;
    }
    /**
     * @return the residence
     * /
    public String getResidence() {
        return residence;
    }
    /**
     * @param residence the residence to set
     * /
    public void setResidence(String residence) {
        this.residence = residence;
    }
    */

    /**
     * @return the friend
     */
    public String getFriend() {
        return getProp(FOAF.knows);
    }

    public String getFriend(String lang) {
        return getProp(FOAF.knows, lang);
    }

    public List<String> getFriends() {
        return getProps(FOAF.knows);
    }

    public List<String> getFriends(String lang) {
        return getProps(FOAF.knows, lang);
    }

    /**
     * @param friend the friend to set
     */
    public void setFriend(String friend) {
        resource.addProperty(FOAF.knows, friend);
    }

    public void setFriend(String friend, String lang) {
        resource.addProperty(FOAF.knows, friend, lang);
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    /**
     * @return the uri
     */
    public String getUri() {
        return uri;
    }
}
